package com.tsingteng.cosfun.ui.login;


import android.os.Bundle;

import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;

import com.tsingteng.cosfun.R;
import com.tsingteng.cosfun.base.BaseFragment;
import com.tsingteng.cosfun.widget.TitleView;

import butterknife.BindView;


/**
 * 描述 :
 * 创建人: 肖利.
 * 时间:2018/8/29
 * qq(872179966)
 */

public class LogonAgreementFragment extends BaseFragment {
    @BindView(R.id.webView)
    WebView webView;
    @BindView(R.id.tv_title_view)
    TitleView titleView;
    private int agementType;//1用户协议  2 用户营私
    private String url;

    public static LogonAgreementFragment newInstance(int agementType) {
        LogonAgreementFragment fragment = new LogonAgreementFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("agementType",agementType);
        fragment.setArguments(bundle);
        return fragment;
    }


    @Override
    protected void initView() {
        if (agementType == 1) {
            titleView.setTitle("用户协议");
            url = "file:///android_asset/useragement.html";

        }else {
            titleView.setTitle("隐私政策");
            url = "file:///android_asset/intimate.html";
        }

        titleView.setIvLeft(R.drawable.back_big_icon);
        titleView.setOnTitleClickListener(new TitleView.OnTitleClickListener() {
            @Override
            public void onLeft() {
                getActivity().finish();
            }

            @Override
            public void onRight() {

            }

            @Override
            public void onRight2() {

            }
        });
        init();
    }

    @Override
    protected void getBundle(Bundle bundle) {
        agementType = bundle.getInt("agementType");
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_agreement_logon;
    }

    private void init() {

        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
            }
        });
        WebSettings webSettings = webView.getSettings();
        if (webSettings != null) {
            webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);//缓存
            webSettings.setJavaScriptEnabled(true);
            webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
            webSettings.setDomStorageEnabled(true);
            webSettings.setDatabaseEnabled(true);
            webSettings.setAppCacheEnabled(true);
            webSettings.setAllowFileAccess(true);
            webSettings.setSavePassword(true);
            webSettings.setDomStorageEnabled(true);
            webSettings.setDefaultTextEncodingName("gb2312");
        }
        webView.loadUrl(url);

        webView.getSettings().setDefaultTextEncodingName("utf-8");
    }
}
